import originAxios from 'axios'

export default function axios(option) {
  return new Promise((resolve,reject)=>{
    // 1.创建axios实例
    const instance = originAxios.create({
      baseURL: 'http://localhost:8088',
      timeout: 5000
    });

    // 配置请求和响应拦截
    instance.interceptors.request.use( config => {
      config.headers.Authorization = localStorage.getItem('Authorization')
      return config
    } ,err => {
      return err
      })

    instance.interceptors.response.use(response => {
      return response.data
    }, err => {
      console.log('来到了response拦截failure中');
      console.log(err)
      if (err && err.response) {
        switch (err.response.status) {
          case 400:
            err.message = '请求错误'
            break
          case 401:
            err.message = '未授权的访问'
            break
        }
      }
      return err
    })





    // 2.传入对象进行网络请求
    instance(option).then(res => {
      resolve(res)
    }).catch( err =>{
      reject(err)
    })
  })
}
